<template>
	<div style="height: 100%">
		<div id="selectOne">
			<cus-allcard style="padding-bottom:0px" :showTitle="false">
				<div class="topBody">
					<div class="topBodyItem"></div>
					<div class="topBodyItem">
						<span class="itemSpan">查询日期</span>
						<Date-picker
							type="month"
							v-model="dataForm.tabledate"
							:clearable="false"
							placeholder="请选择日期"
							@on-change="getStartAndEnd($event)"
							class="itemImport"
						></Date-picker>
					</div>
					<div class="topBodyItem">
						<cus-Button type="primary" @onClick="getYueHuoShiAndYueFoodApi(true)"
							>查询</cus-Button
						>
					</div>
					<div class="topBodyItem">
						<cus-Button type="primary" @onClick="saveData" v-permission="30">保存</cus-Button>
					</div>
					<div class="topBodyItem">
						<cus-Button type="primary" @onClick="getExcelExportByIdApi" v-permission="50"
							>导出</cus-Button
						>
					</div>
					<div class="topBodyItem">
						<cus-Button type="primary" @onClick="reportExpense" v-permission="40"
							>上报</cus-Button
						>
					</div>
				</div>
			</cus-allcard>
		</div>
		<cus-allcard
			style="margin-top:20px;padding:24px;height: calc(100% - 92px);overflow:auto"
			:showTitle="false"
		>
			<expense-report
				ref="repenseReport"
				:dateSelect="dataForm.tabledate"
				:reportOption="reportOption"
				@freshData="freshData"
			></expense-report>
		</cus-allcard>
	</div>
</template>
<script>
import expenseReport from './compnents/expenseReport.vue'
import {
	getMonthMealsList,
	reportMonthMealApply,
	monthlyMealsReportExcel
} from '@/api/businessAccountingApi/essentialInformationApi'
export default {
	components: { expenseReport },
	data() {
		return {
			// 查询条件的集合
			selectForm: {
				year: null, // 账薄年度
				yearList: [], // 账薄年度列表
				accountbookId: 1, // 账簿id
				yuehuoshiId: null, // 月伙食公布表id
				selectMonth: null // 查询日期
			},
			dataForm: {
				tabledate: new Date(
					new Date().getFullYear(),
					new Date().getMonth() - 1,
					1
				)
			},
			reportOption: {}
		}
	},
	mounted() {
		this.optionDate()
		this.getReportDataDetail()
	},
	methods: {
		// 限制日期封装方法
		optionDate() {
			if (this.selectForm.year == new Date().Format('yyyy')) {
				// 本年度 - 限制：1月~当前月（不能跨年）
				if (new Date().Format('MM') == '01') {
					this.timeOptions = {
						disabledDate(value) {
							return (
								value < new Date(new Date().Format('yyyy') - 1 + '-12-31') ||
								value > new Date(new Date().Format('yyyy') + '-01-31')
							)
						}
					}
				} else {
					let timess = new Date().Format('yyyy-MM-dd')
					let yearTime = new Date(timess).Format('yyyy')
					let monthTime = new Date(timess).Format('MM') - 1 //上个月
					let enddataTime = new Date(yearTime, monthTime, 0).getDate() // 获取上个月最后一天的日 28或29或30或31....
					let endTimes = yearTime + '-' + monthTime + '-' + enddataTime // 月末
					this.timeOptions = {
						disabledDate(value) {
							return (
								value < new Date(new Date().Format('yyyy') - 1 + '-12-31') ||
								value > new Date(endTimes)
							)
						}
					}
				}
			} else {
				// 非本年度 - 限制：1月~12月（不能跨年）
				let startOption = Number(this.selectForm.year) - 1 + '-12-31' // 限制开始能点日期 必须先建变量存储，才能放进new Date()
				let endOption = Number(this.selectForm.year) + '-12-31' // 限制结束能点日期 必须先建变量存储，才能放进new Date()
				this.timeOptions = {
					disabledDate(value) {
						return value < new Date(startOption) || value > new Date(endOption)
					}
				}
			}
		},
		saveYueHuoShiAndYueFoodApi() {},
		accountbookIdChange() {},
		generateTableChange() {},
		getYueHuoShiAndYueFoodApi(flag) {
			this.getReportDataDetail(this.dataForm.tabledate.Format('yyyy-MM'),flag)
		},
		QuanPing() {},
		getExcelExportByIdApi() {
			let params = {}
			params.basMonthlyMealsApplyId = this.reportOption.basMonthlyMealsApplyId
			monthlyMealsReportExcel(params).then((res) => {
				this.$cu.hideLoading(1)
				let msg = ''
				let downloadName = ''
				msg = '伙食费领取报表导出成功'
				downloadName = '伙食费领取报表'
				this.$Message.success(msg)
				let blob = new Blob([res], { type: `application/vnd.ms-excel` })
				let downloadElement = document.createElement('a')
				let href = window.URL.createObjectURL(blob)
				downloadElement.href = href
				downloadElement.download = downloadName
				document.body.appendChild(downloadElement)
				downloadElement.click()
				document.body.removeChild(downloadElement)
				window.URL.revokeObjectURL(href)
				resolve()
			})
		},
		saveData() {
			this.$cu.debounce(() => {
				this.$refs.repenseReport.saveData(this.dataForm.tabledate)
			})
			
		},
		freshData(date) {
			let currenDate = new Date(date).Format('yyyy-MM')
			this.getReportDataDetail(currenDate)
		},
		getStartAndEnd(date) {
			this.getReportDataDetail(date)
		},
		getReportDataDetail(date,flag) {
			let params = {}
			if (date) {
				params.applyYearMonth = date
			} else {
				let today = new Date()
				today.setMonth(new Date().getMonth() - 1)
				params.applyYearMonth = today.Format('yyyy-MM')
			}
			getMonthMealsList(params).then((res) => {
				if (res.retCode == 2000) {
          if(flag){
            this.$Message.success('查询成功')
          }
		  console.log(res.data,"abcd");
			this.reportOption = res.data
				}
			})
		},
		reportExpense() {
			this.$refs.repenseReport.reportExpense(this.dataForm.tabledate)
		}
	}
}
</script>
<style lang="less" scoped>
@import './index.less';
</style>
